package com.tiantian.quanminxiu.utils.gift;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.model.GiftModel;
import com.tiantian.quanminxiu.model.RunwayModel;
import com.tiantian.quanminxiu.utils.APPUtils;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.concurrent.LinkedBlockingQueue;

public class BigGiftShowManager
{
    private ImageView imageFrameCustomView;//跑道的容器
    private Activity cxt;//上下文
    private LinkedList<GiftModel> giftQueue;//大礼物队列

    private final static int GET_QUEUE_GIFT = 0;//从队列中获取礼物
    private final static int SHOW_GIFT_ANIM = 3;//大礼物gif

    private giftHandler handler = new giftHandler(cxt) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GET_QUEUE_GIFT://如果是从队列中获取礼物实体的消息
                    if (giftQueue.isEmpty())
                    {
                        return;
                    }
                    try
                    {
                        GiftModel vo = giftQueue.getFirst();
                        if (vo != null&&imageFrameCustomView.getVisibility()==View.GONE) {//如果从队列中获取的礼物不为空，那么就将礼物展示在界面上
                            imageFrameCustomView.setVisibility(View.VISIBLE);
                            Message giftMsg = new Message();
                            giftMsg.obj = giftQueue.removeFirst();
                            giftMsg.what = SHOW_GIFT_ANIM;
                            handler.sendMessage(giftMsg);
                        } else {
                            handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//如果这次从队列中获取的消息是礼物是空的，则一秒之后重新获取
                        }
                    }catch (NoSuchElementException elementException){
                        handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//如果这次从队列中获取的消息是礼物是空的，则一秒之后重新获取
                        elementException.printStackTrace();
                    }
                    break;
                case SHOW_GIFT_ANIM:
                    try
                    {
                        GiftModel obj = (GiftModel) msg.obj;
                        final FrameAnimation frameAnimation = new FrameAnimation(imageFrameCustomView, getAnimInts(obj.getGiftName()), 100, false);
                        frameAnimation.setAnimationListener(new FrameAnimation.AnimationListener() {
                            @Override
                            public void onAnimationStart() {

                            }

                            @Override
                            public void onAnimationEnd() {
                                frameAnimation.release();
                                imageFrameCustomView.setVisibility(View.GONE);
                                imageFrameCustomView.setImageResource(0);
                            }

                            @Override
                            public void onAnimationRepeat() {

                            }
                        });
                    }catch (Exception e){
                        imageFrameCustomView.setImageResource(0);
                        imageFrameCustomView.setVisibility(View.GONE);
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public BigGiftShowManager(Activity cxt, ImageView imageView) {
        this.cxt = cxt;
        this.imageFrameCustomView = imageView;
        this.giftQueue = new LinkedList<>();
        this.imageFrameCustomView.setVisibility(View.GONE);
    }

    //开始显示礼物
    public void showGiftAnim() {
        handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//轮询队列获取礼物
    }

    //放入礼物到队列
    public void addGiftWay(GiftModel rm){
        giftQueue.add(rm);
        showGiftAnim();
    }

    //释放内存
    public void leakHandle(){
        handler.removeCallbacksAndMessages(null);
        cxt = null;
    }

    private static class giftHandler extends Handler {
        private final WeakReference<Activity> mActivity;

        public giftHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mActivity.get() == null) {
                return;
            }
        }
    }

    public int[] getAnimInts(String name){
        int[] resIds = new int[20];;
        Resources res = cxt.getResources();
        String packageName = cxt.getPackageName();
        String title = "";
        switch (name){
            case "跑车":
                title = "car";
                break;
            case "飞机":
                title = "airplane";
                break;
            case "航母":
                title = "aircraft";
                break;
            case "别墅":
                title = "villadom";
                break;
            case "游艇":
                title = "yacht";
                break;
        }
        if (TextUtils.isEmpty(title))
        {
            throw new RuntimeException("找不到这个礼物信息");
        }
        for (int i = 0; i < resIds.length; i++) {
            String resName = title + (i + 1);
            int imageResId = res.getIdentifier(resName, "drawable", packageName);
            resIds[i] = imageResId;
        }
        return resIds;
    }
}
